//
//  TJInputPasswordControntroller.m
//  Teleport
//
//  Created by hc-101 on 2020/6/26.
//  Copyright © 2020 xtjun. All rights reserved.
//

#import "TJInputPasswordControntroller.h"
 
@interface TJInputPasswordControntroller ()<UITextFieldDelegate>

@property (strong, nonatomic) NSMutableArray *datas;
@property (weak, nonatomic) UIView *maskView;
@property (weak, nonatomic) UIView *contentView;
@property (weak, nonatomic) UITextField *passwordTextField;

@end

  
@implementation TJInputPasswordControntroller



- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.modalPresentationStyle = UIModalPresentationCustom;
    }
    return self;
}
- (void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = UIColor.tj_alertMaskColor;
        self.contentView.tj_y = KScreenHeight - self.contentView.tj_height;
    } completion:^(BOOL finished) {
    }];
    
    [IQKeyboardManager sharedManager].enable = YES;

}
- (void)viewDidLoad {
    [super viewDidLoad];
    
    _datas = @[].mutableCopy;
 
    
    self.view.backgroundColor = UIColor.clearColor;
    
    UIView *maskView = [UIView tj_addToView:self.view];
    maskView.backgroundColor = [UIColor.tj_alertMaskColor colorWithAlphaComponent:0];
    maskView.frame = self.view.bounds;
    
    UIView *contentView = [UIView tj_addToView:maskView];
    contentView.backgroundColor = UIColor.whiteColor;
    UIView *headerView = [UIView tj_addToView:contentView];
    [headerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.offset(0);
        make.height.offset(50);
        make.top.offset(0);
    }];
 
    
    UILabel *nameLabel = [UILabel lableWithSuperView:headerView font:MediumFont(16) color:UIColor_Hex(0x272727) title:@"安全验证".localizedString textAlignment:NSTextAlignmentLeft];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.offset(0);
    }];
    
    UIButton *dismissButton = [UIButton buttonWithSuperView:headerView font:UIFont_Px(15) color:UIColor_Hex(0x272727) title:@"关闭".localizedString];
    [dismissButton addTarget:self action:@selector(didClickDismissButton) forControlEvents:UIControlEventTouchUpInside];
    [dismissButton setContentHorizontalAlignment:(UIControlContentHorizontalAlignmentRight)];
     [dismissButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(nameLabel);
        make.width.height.offset(50);
        make.right.offset(-12);
    }];
    
    
    UILabel *passwordLabel = [UILabel lableWithSuperView:contentView font:MediumFont(14) color:UIColor_Hex(0x111111) title:@"资金密码".localizedString textAlignment:NSTextAlignmentLeft];
       [passwordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
           make.top.equalTo(headerView.mas_bottom).offset(8);
           make.left.offset(26);
       }];
    
    
    UITextField *passwordTextField = [UITextField tj_addToView:contentView];
    passwordTextField.placeholder = @"请输入资金密码".localizedString;
    passwordTextField.secureTextEntry = YES;
    passwordTextField.font = UIFont_Px(15);
    passwordTextField.clearButtonMode = UITextFieldViewModeWhileEditing;
    passwordTextField.keyboardType = UIKeyboardTypeNumberPad;
    passwordTextField.returnKeyType = UIReturnKeyDone;
    passwordTextField.delegate = self;
    [passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordLabel);
        make.top.equalTo(passwordLabel.mas_bottom).offset(6);
        make.height.offset(34);
        make.right.offset(-26);
    }];
    
    UIView *passwordLineView = [UIView tj_addToView:contentView];
    passwordLineView.backgroundColor = UIColor_Hex(0xE5E5E5);
    [passwordLineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(passwordTextField);
        make.right.equalTo(passwordTextField);
        make.height.offset(1);
        make.top.equalTo(passwordTextField.mas_bottom).offset(2);
    }];
    
  
       UILabel *forgetPasswordLabel = [UILabel lableWithSuperView:contentView font:UIFont_Px(12) color:UIColor_Hex(0x882E8) title:@"忘记密码".localizedString textAlignment:NSTextAlignmentLeft];
    [forgetPasswordLabel addTapGesturesWithTarget:self action:@selector(tapForgetPasswordLabel)];
          [forgetPasswordLabel mas_makeConstraints:^(MASConstraintMaker *make) {
              make.top.equalTo(passwordLineView.mas_bottom).offset(6);
              make.left.equalTo(passwordTextField);
          }];
    
 
    UIButton *buyButton = [UIButton buttonWithSuperView:contentView font:UIFont_Px(15) color:UIColor.whiteColor title:@"确定".localizedString];
    buyButton.backgroundColor = UIColor.tj_mainColor;
    [buyButton shearRoundedCornersWithRadiu:18];
    [buyButton addTarget:self action:@selector(didClickBuyButton) forControlEvents:UIControlEventTouchUpInside];
    [buyButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(200);
         make.height.offset(36);
        make.centerX.offset(0);
        make.top.equalTo(forgetPasswordLabel.mas_bottom).offset(25);
    }];
    
 
    
    CGFloat totalHeight = 200 + TabbarSafeBottomMargin + 50;
    contentView.frame = CGRectMake(0, KScreenHeight, KScreenWidth, totalHeight);
    
    _maskView = maskView;
    _contentView = contentView;
    _passwordTextField = passwordTextField;
    
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [self didClickBuyButton];
    return YES;
}

- (void)tapForgetPasswordLabel {
    
    [self dismissComplete:^{
        if (self.forgetPasswordBlock) {
            self.forgetPasswordBlock();
        }
    }];
}

- (void)didClickDismissButton {
    [self dismissComplete:nil];
}

- (void)didClickBuyButton {
    if (!_passwordTextField.text.length) {
        [TJHud showAutoHideMessage:_passwordTextField.text];
        return;
    }
    
    [self dismissComplete:^{
        if (self.selectComplete) {
            self.selectComplete(self.passwordTextField.text);
        }
    }];
    
}

- (void)dismissComplete:(void (^)(void))complete {
    
    [UIView animateWithDuration:0.25 animations:^{
        self.maskView.backgroundColor = [UIColor.blackColor colorWithAlphaComponent:0];
        self.contentView.tj_y = KScreenHeight;
    } completion:^(BOOL finished) {
        [self dismissViewControllerAnimated:NO completion:^{
            if (complete) complete();
        }];
    }];
}



 


@end
